//
//  LBContnerView.m
//  LBNestPageListView
//
//  Created by liubo on 2022/11/27.
//

#import "LBContnerView.h"
#import <LXBPageView/LXBPageView.h>
#import "CollectionViewController.h"
#import <LBPageMenu/LBPageMenu.h>
#import <Masonry/Masonry.h>

@interface LBContnerView () <LXBPageViewDelegate, LXBPageViewDataSource, LBPageMenuDelegate>

@property (nonatomic, strong) LBPageMenu *menu;

@property (nonatomic, strong) LXBPageView *pageView;

@property (nonatomic, strong)   CollectionViewController *vc;

@property (nonatomic, strong) CollectionViewController *vc2;

@end

@implementation LBContnerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
        [self setUpContraints];
    }
    return self;
}

- (void)setUpUI
{
    NSArray *array = @[@"标题", @"哈哈哈"];
    [self.menu setItems:array selectedItemIndex:0];
    [self addSubview:self.menu];
    [self addSubview:self.pageView];
    [self.pageView reloadData];
}

- (void)setUpContraints
{
    [self.menu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(40);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - selectItemAtIndex

- (void)selectItemAtIndex:(NSInteger)index
{
    [self.pageView showSubViewWithIndex:index];
}

#pragma mark - LXBPageViewDelegate, LXBPageViewDataSource>

- (NSInteger)subViewsNumberOfPageView:(LXBPageView *)pageView
{
    return 2;
}
- (UIView *)subViewForPageView:(LXBPageView *)pageView atIndex:(NSInteger)index
{
    if (index == 1) {
        return self.vc.view;
    }
    return self.vc2.view;
}

- (void)scrollViewInPageView:(LXBPageView *)pageView didScrollToIndex:(NSInteger)index
{
    
}

- (void)scrollViewInPageView:(LXBPageView *)pageView didScroll:(UIScrollView *)scrollView
{
    [self.menu beginMoveTrackerFollowScrollView:scrollView];
}

#pragma mark - lazy load

- (LBPageMenu *)menu
{
    if (!_menu) {
        LBPageMenuConfiguration *configuration = [[LBPageMenuConfiguration alloc] init];
        configuration.unSelectedTitleColor = [UIColor cyanColor];
        configuration.selectedTittleColor = [UIColor magentaColor];
        ///指示条滚动风格，这里配置拉伸风格（默认是普通滚动）
        configuration.indicatorStyle = LBPageMenuIndicatorStyleStretch;

        _menu = [[LBPageMenu alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 40) configuration:configuration];
        _menu.delegate = self;
        _menu.backgroundColor = [UIColor lightGrayColor];
    }
    return _menu;
}

- (LXBPageView *)pageView
{
    if (!_pageView) {
        _pageView = [[LXBPageView alloc] initWithFrame:CGRectMake(0, 40, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - 40)];
        _pageView.dataSource = self;
        _pageView.delegate = self;
    }
    return _pageView;
}

- (CollectionViewController *)vc
{
    if (!_vc) {
        _vc = [[CollectionViewController alloc] init];
    }
    return _vc;
}

- (CollectionViewController *)vc2
{
    if (!_vc2) {
        _vc2 = [[CollectionViewController alloc] init];
    }
    return _vc2;
}

@end
